package com.yjt.common.tools;

/**
 * <p>Title: PMSW</p>
 * <p>Description: PMS Web Edition</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Talent information Solutions Ltd.</p>
 * @author bobdog chan
 * @version 1.0
 */

public class SQLUtil {
    public SQLUtil() {
    }

    public static String appendWhereClause(String oriSql, String subSql) {
        if (subSql == null || subSql.equals("")) {
            return oriSql;
        }

        StringBuffer temp = new StringBuffer();
        boolean hasWhere = oriSql.indexOf("WHERE") > 0;
        int index = oriSql.indexOf("GROUP BY");
        if (index <= 0)
            index = oriSql.indexOf("ORDER BY");
        if (index <= 0) {
            temp.append(oriSql);
            if (hasWhere)
                temp.append(" AND ");
            else
                temp.append(" WHERE ");
            temp.append(subSql);
        }
        else {
            temp.append(oriSql.substring(0, index));
            if (hasWhere)
                temp.append(" AND ");
            else
                temp.append(" WHERE ");
            temp.append(subSql);
            temp.append(" ");
            temp.append(oriSql.substring(index));
        }
        return temp.toString();
    }

}